import { setGlobal } from '@swda/utils'

//-----------------------------------------------------------------------------
// SWDA_Window_Command_4Arrows
//
// The superclass of windows for selecting a command.

function SWDA_Window_Command_4Arrows() {
    this.initialize.apply(this, arguments);
}

SWDA_Window_Command_4Arrows.prototype = Object.create(SWDA_Window_Selectable_4Arrows.prototype);
SWDA_Window_Command_4Arrows.prototype.constructor = SWDA_Window_Command_4Arrows;

SWDA_Window_Command_4Arrows.prototype.initialize = function (x, y) {
    this.clearCommandList();
    this.makeCommandList();
    let width = this.windowWidth();
    let height = this.windowHeight();
    SWDA_Window_Selectable_4Arrows.prototype.initialize.call(this, x, y, width, height);
    this.refresh();
    this.select(0);
    this.activate();
};

SWDA_Window_Command_4Arrows.prototype.maxItems = function () {
    return this._list.length;
};

SWDA_Window_Command_4Arrows.prototype.clearCommandList = function () {
    this._list = [];
};

SWDA_Window_Command_4Arrows.prototype.makeCommandList = function () {
};

SWDA_Window_Command_4Arrows.prototype.addCommand = function (name, symbol, enabled, ext) {
    if (enabled === undefined) {
        enabled = true;
    }
    if (ext === undefined) {
        ext = null;
    }
    this._list.push({ name: name, symbol: symbol, enabled: enabled, ext: ext });
};

SWDA_Window_Command_4Arrows.prototype.commandName = function (index) {
    return this._list[index].name;
};

SWDA_Window_Command_4Arrows.prototype.commandSymbol = function (index) {
    return this._list[index].symbol;
};

SWDA_Window_Command_4Arrows.prototype.isCommandEnabled = function (index) {
    return this._list[index].enabled;
};

SWDA_Window_Command_4Arrows.prototype.currentData = function () {
    const index = this.index()
    return index >= 0 ? this._list[index] : null;
};

SWDA_Window_Command_4Arrows.prototype.isCurrentItemEnabled = function () {
    return this.currentData() ? this.currentData().enabled : false;
};

SWDA_Window_Command_4Arrows.prototype.currentSymbol = function () {
    return this.currentData() ? this.currentData().symbol : null;
};

SWDA_Window_Command_4Arrows.prototype.currentExt = function () {
    return this.currentData() ? this.currentData().ext : null;
};

SWDA_Window_Command_4Arrows.prototype.findSymbol = function (symbol) {
    for (let i = 0; i < this._list.length; i++) {
        if (this._list[i].symbol === symbol) {
            return i;
        }
    }
    return -1;
};


SWDA_Window_Command_4Arrows.prototype.selectSymbol = function (symbol) {
    const index = this.findSymbol(symbol);
    if (index >= 0) {
        this.select(index);
    } else {
        this.select(0);
    }
};


SWDA_Window_Command_4Arrows.prototype.isOkEnabled = function () {
    return true;
};

SWDA_Window_Command_4Arrows.prototype.callOkHandler = function () {
    const symbol = this.currentSymbol();
    if (this.isHandled(symbol)) {
        this.callHandler(symbol);
    } else if (this.isHandled('ok')) {
        SWDA_Window_Selectable_4Arrows.prototype.callOkHandler.call(this);
    } else {
        this.activate();
    }
};

SWDA_Window_Command_4Arrows.prototype.refresh = function () {
    this.clearCommandList();
    this.makeCommandList();
    this.createContents();
    SWDA_Window_Selectable_4Arrows.prototype.refresh.call(this);
};



setGlobal('SWDA_Window_Command_4Arrows', SWDA_Window_Command_4Arrows)